package com.xuyiwei.mayi.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

/**
 * Created by xuyiwei on 2019/4/18.
 */
@Service
public class UserService {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    public String register(String name,String pwd){
        String sql = "INSERT INTO uuid (id) VALUE (NULL )";
        jdbcTemplate.update(sql);
        String idSql = "SELECT last_insert_id()";
        Long id = jdbcTemplate.queryForObject(idSql,Long.class);
        String tableName = getTablename(id);
        String insertSql = "insert into "+tableName+" (id,name,pwd) VALUE ("+id+",'"+name+"','"+pwd+"')";
        jdbcTemplate.update(insertSql);
        System.out.println("注册成功");
        return "success";
    }

    public String getName(Long id){
        String tableName = getTablename(id);
        String sql = "SELECT name from "+tableName+" where id = "+id+"";
        try{
            String name = jdbcTemplate.queryForObject(sql,String.class);
            return name;
        }catch (Exception e){
            return null;
        }
    }

    public String getTablename(Long id){
        return "user"+(id%3);
    }

    public void insertData( ) {
        for(int i=0;i<1000;i++){
            String sql ="insert into index_test values (NULL ,'姓名"+i+"','部门"+(i%20)+"',"+i+",'普通索引"+(i%20)+"',"+(i%20)+" )";
            jdbcTemplate.update(sql);
        }
    }
}
